#include "tcp.h"
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#ifndef _WIN32
#include <netinet/in.h>
#ifdef _XOPEN_SOURCE_EXTENDED
#include <arpa/inet.h>
#endif
#include <sys/socket.h>
#endif

#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>
#include "util.h"
#include <list>


list<bufferevent *> listServ;




bool isConnect(string ip)
{
    for(auto i: listServ)
    {
        string addr  = getPeerIP(i);
        if(addr == ip)
        {
            return true;
        }
    }

    return false;
}

void wirteToServer(string ip,const void *data, size_t size)
{
    for(auto i: listServ)
    {
        string addr  = getPeerIP(i);
        if(addr == ip)
        {
            bufferevent_write(i, data, size);
        }
    }
}

void wirteToServer(string ip,const void *data, size_t size, string& recvMsg)
{
    for(auto i: listServ)
    {
        string addr  = getPeerIP(i);
        if(addr == ip)
        {   
            string strSend;
            unsigned char* dateArr = (unsigned char*)data;
            for(int i =0; i < size; i++)
            {
                strSend += unsignedCharToHex(dateArr[i]);
            }
            
            printf("send arr:%s\n",strSend.c_str());
            bufferevent_write(i, data, size);
            //   char arr[1024] ={0};
            // bufferevent_read(i, arr, sizeof(arr));
            // recvMsg = arr;
            // printf("recv:%s\n", recvMsg.c_str());
        }
    }
}

void write_cb(struct bufferevent *bev, void *arg)
{
    //printf("我是客户端的写回调函数，没卵用\n");
}
void event_cb(struct bufferevent *bev, short events, void * arg)
{
    if(events & BEV_EVENT_EOF)
    {
        printf("connection closed \n");
    }
    else if(events & BEV_EVENT_ERROR)
    {
        printf("some other error\n");
    }
    else if(events & BEV_EVENT_CONNECTED)
    {
        printf("已经连接了服务器.....\n");
        listServ.push_back(bev);
        return ;
    }
    
    //释放资源
    bufferevent_free(bev);
}
//客户端与用户教会，从终端读取数据给服务器
void read_terminal(evutil_socket_t fd, short what, void *arg)
{
    //读数据
    char buf[1024] = {0};
    int len = read(fd, buf, sizeof(buf));
    
    struct bufferevent* bev = (struct bufferevent*)arg;
    
    //发送数据
    bufferevent_write(bev, buf, len+1);
}

void connect(string ip, int port,readCb read_cb)
{
    struct event_base* base = NULL;
    base = event_base_new();

    int fd = socket(AF_INET, SOCK_STREAM, 0);
    
    //通信的fd放到bufferevent中
    struct bufferevent *bev = NULL;
    bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    
    // init server info
    struct sockaddr_in serv;
    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_port = htons(port);
    inet_pton(AF_INET, ip.c_str(), &serv.sin_addr.s_addr);
    
    //连接服务器
    bufferevent_socket_connect(bev, (struct sockaddr*)&serv, sizeof(serv));
    
    //设置回调
    bufferevent_setcb(bev, read_cb, write_cb, event_cb, NULL);

    
    //设置回调生效
    bufferevent_enable(bev, EV_READ);
    
    //创建事件
    struct event* ev = event_new(base, STDIN_FILENO, EV_READ | EV_PERSIST,read_terminal, bev);
    
    //添加事件
    event_add(ev, NULL);
    event_base_dispatch(base);
    event_free(ev);
    event_base_free(base);
}